package model;
// 通过这个BlogDao 完成 针对 blog表的操作 ;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {

    // 1. 新增博客 (发布博客时)
    public void insert(Blog blog){
        Connection connection = null ;
        PreparedStatement statement = null;
        try {
            //1. 建立连接
             connection = DBUtil.getConnection();
            // 2. 构造sql
             String sql = "insert into blog values(null,?,?,now() ,?)";
             statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            // 3. 执行sql
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            // 4. 回收资源
            DBUtil.close(connection,statement,null) ;
        }
    }

  // 2.查询博客列表(在博客列表页里,把博客表里的数据拿到)
    public List<Blog> getBlogs(){
        Connection connection = null ;
        PreparedStatement statement = null;
        ResultSet resultSet = null ;
         List<Blog> list = new ArrayList<>();
        try{
            //1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造sql
            String sql = "select * from blog";
            statement = connection.prepareStatement(sql)   ;
            resultSet = statement.executeQuery() ;
            while(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                // 对正文进行截断
                String content = resultSet.getString("content");
                if(content.length()>100){
                    content = content.substring(0,100) + "...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
             list.add(blog);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            DBUtil.close(connection,statement, resultSet);
        }

        return list ;
    }
   // 3. 根据博客 id 查询指定的博客
    public Blog getBlog(int blogId){
        Connection connection = null ;
        PreparedStatement statement = null;
        ResultSet resultSet = null ;
        try{
             connection = DBUtil.getConnection();
             String sql = "select * form blog where blogId=?";
             statement =connection.prepareStatement(sql) ;
             statement.setInt(1, blogId);
             resultSet = statement.executeQuery();
             if (resultSet.next()){
              Blog blog = new Blog();
              blog.setUserId(blogId);
              blog.setTitle(resultSet.getString("title"));
              blog.setContent(resultSet.getString("content"));
              blog.setPostTime(resultSet.getTimestamp("postTime"));
              blog.setUserId(resultSet.getInt("userId"));
              return blog ;
             }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }

        return null ;
    }

    //4. 根据博客 Id , 删除指定博客
    public void delete(int blogId){
        Connection connection = null ;
        PreparedStatement statement = null;
        try{
            connection = DBUtil.getConnection();
            String sql = "delete from blog where blogId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            statement.executeUpdate() ;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,null);
        }

    }

}
